package com.cxf.windex.config;

import com.cxf.windex.base.secruity.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * 配置整体站点安全
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /**
     * If subclassed this will potentially override subclass
     * configure(HttpSecurity)
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // super.configure(http);
        http.csrf().disable();
        http.headers().frameOptions().disable();
        http.formLogin()
            // 登录页面的位置
            .loginPage("/login")
            .defaultSuccessUrl("/index",true)
            .and()
            .logout()
            .logoutSuccessUrl("/login")
            .invalidateHttpSession(true)
            // 退出时删除cookie
            .deleteCookies("JSESSIONID")
            .and()
            .authorizeRequests()
            .antMatchers("/**/*.css","/**/*.png","/**/*.jpg","/**/*.gif","/*.ico", "/upload/**","/img/**","/lib/**", "/**/*.js","/api/**","/*/api/**","/user/**","/login","/lvshi/**","/","/lawyer/**","/content/**"
            ,"/money/**","/log/**","/register","/platform_api/**","/weixin/**","/mina/**","/api*/**"
            ) // 放开"/api/**",
            // 通过oauth2.0来鉴权
            .permitAll()
            .and()
            .authorizeRequests()
            .antMatchers("/**")
            .authenticated();

        // http.logout().logoutSuccessUrl("/index"); // 退出默认跳转页面 (5)

        // 禁用缓存
        http.headers().cacheControl();
    }
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        //auth.userDetailsService(detailsService).passwordEncoder(new BCryptPasswordEncoder());
        //auth.authenticationProvider(ideaAuthenticationProvider);
    }
    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }
}
